
use $datapath/clean/main_data.dta, clear 

************************************************
* Create variable labels: 

	label var Qp "phys"
	label var Np "phys holdings"
	label var Qe "ebook"
	label var Ne "ebook holdings"

			
**********************************************************************
** create jump variables


************************************
** ebook jumps 
	
	gsort lno year 
	
** levels: 	
	gen ex = Ne-Ne[_n-1] if lno==lno[_n-1] 
	su ex, de
		local etd = r(p90)

** jump cutoff: 
gsort lno year
	gen ee = (Ne-Ne[_n-1] >`etd') & lno==lno[_n-1] & (Ne+Ne[_n-1]~=.) & (Ne[_n+1]-Ne >=0)
	
******************


******************
** create IVs

	gsort lno year
		by lno: gen ive =sum(ee) 
		label var ive "ebook jump"

** First jump year:
	gen xe = year if ive==1  
	egen Ymin = min(xe), by(lno)

** total number of jumps:  
	egen total_jumps = sum(ee), by(lno)	
	
**********************************************************************



**********************************************************************
** Run regressions - IVs:	

** Set up observations we want to include 
	gen ifstatement = (ive <2 & year<=Ymin+1 & year >= Ymin-2) | total_jumps==0

** Regressions: 	
	xtset lno year
	eststo clear
	
** IV - just Ne
	eststo: xtivreg Qp (Ne =  ive)  i.year if ifstatement==1, fe vce(cluster lno)	
	eststo: xtivreg Qe (Ne =  ive)  i.year if ifstatement==1, fe vce(cluster lno)	

** IV - control for Np 
	eststo: xtivreg Qp Np (Ne =  ive)   i.year if ifstatement==1, fe vce(cluster lno)	
	eststo: xtivreg Qe Np (Ne =  ive)  i.year if ifstatement==1, fe vce(cluster lno)	


** create table 
local pattern prefix(/multicolumn{@span}{c}{) suffix(}) span erepeat(/cmidrule(lr){@span})

	esttab,  noomitted mtitles( "Phys" "Ebook" "Phys" "Ebook") drop(*.year _cons) se replace  label nonotes star(* 0.10 ** 0.05 *** 0.01)  nocons


esttab using "$tablepath/table3_circ_IV.tex", noomitted mtitles( "Phys" "Ebook" "Phys" "Ebook") drop(*.year _cons) se replace  label nonotes star(* 0.10 ** 0.05 *** 0.01)  nocons booktabs


